﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bloodbank.Dto;

namespace Bloodbank.Api
{
    public class UserApi
    {
        private BloodbankDbEntities contextDB;
        public UserApi() {
            contextDB = new BloodbankDbEntities();
        }

        public UserDto GetUserById(int id)
        {
            try
            {
                var objUser = contextDB.Users.FirstOrDefault(attr => attr.Id == id);
                UserDto user = new UserDto() { 
                    Id = objUser.Id,
                    Username = objUser.Username,
                    Password = objUser.Password,
                    ConfirmPassword = objUser.ConfimPassword,
                    Email = objUser.Email,
                    FullName = objUser.FullName,
                    Address = objUser.Address,
                    City = objUser.City,
                    State = objUser.State,
                    Gender = (Boolean)objUser.Gender,
                    Phone = objUser.Phone,
                    Birthday = DateTime.Parse(objUser.Birthday.ToString()),
                    BloodGroupId = objUser.IdBloodGroup
                };
                return user;
            }
            catch (Exception ex) {
                return new UserDto();
            }
        }
        public List<UserDto> GetUserList()
        {
            try
            {
                //var objUsers = contextDB.Users.ToList().OrderByDescending(o => o.Id);
                //var objUsers = from objUser in contextDB.Users
                //               join objBloodGroup in contextDB.BloodGroups
                //               on objUser.IdBloodGroup equals objBloodGroup.Id
                //               orderby objUser.Id descending
                //               select new UserDto()
                //               {
                //                   Id = objUser.Id,
                //                   Username = objUser.Username,
                //                   Password = objUser.Password,
                //                   ConfirmPassword = objUser.ConfimPassword,
                //                   Email = objUser.Email,
                //                   FullName = objUser.FullName,
                //                   Address = objUser.Address,
                //                   City = objUser.City,
                //                   State = objUser.State,
                //                   Gender = (Boolean)objUser.Gender,
                //                   Phone = objUser.Phone,
                //                   Birthday = (DateTime)objUser.Birthday,
                //                   BloodGroupId = objUser.IdBloodGroup,
                //                   BloodGroupName = objBloodGroup.BloodGroupName
                //               };

                var objUsers = from objUser in contextDB.Users
                               join objBloodGroup in contextDB.BloodGroups
                               on objUser.IdBloodGroup equals objBloodGroup.Id
                               select new {
                                   objUser.Id,
                                   objUser.Username,
                                   objUser.Password,
                                   objUser.ConfimPassword,
                                   objUser.Email,
                                   objUser.FullName,
                                   objUser.Address,
                                   objUser.City,
                                   objUser.State,
                                   objUser.Gender,
                                   objUser.Phone,
                                   objUser.Birthday,
                                   BloodGroupId = objBloodGroup.Id,
                                   objBloodGroup.BloodGroupName,
                               };

                List<UserDto> lstUsers = new List<UserDto>();
                foreach (var obj in objUsers)
                {
                    UserDto user = new UserDto()
                    {
                        Id = obj.Id,
                        Username = obj.Username,
                        Password = obj.Password,
                        ConfirmPassword = obj.ConfimPassword,
                        Email = !string.IsNullOrEmpty(obj.Email) ? obj.Email : string.Empty,
                        FullName = obj.FullName,
                        Address = !string.IsNullOrEmpty(obj.Address) ? obj.Address : string.Empty,
                        City = !string.IsNullOrEmpty(obj.City) ? obj.City : string.Empty,
                        State = !string.IsNullOrEmpty(obj.State) ? obj.State : string.Empty,
                        Gender = (Boolean)obj.Gender,
                        Phone = !string.IsNullOrEmpty(obj.Phone) ? obj.Phone : string.Empty,
                        Birthday = (DateTime)obj.Birthday,
                        BloodGroupId = obj.BloodGroupId,
                        BloodGroupName = obj.BloodGroupName

                        //Id = obj.Id,
                        //Username = obj.Username,
                        //Password = obj.Password,
                        //ConfirmPassword = obj.ConfirmPassword,
                        //Email = !string.IsNullOrEmpty(obj.Email) ? obj.Email : string.Empty,
                        //FullName = obj.FullName,
                        //Address = !string.IsNullOrEmpty(obj.Address) ? obj.Address : string.Empty,
                        //City = !string.IsNullOrEmpty(obj.City) ? obj.City : string.Empty,
                        //State = !string.IsNullOrEmpty(obj.State) ? obj.State : string.Empty,
                        //Gender = (Boolean)obj.Gender,
                        //Phone = !string.IsNullOrEmpty(obj.Phone) ? obj.Phone : string.Empty,
                        //Birthday = (DateTime)obj.Birthday,
                        //BloodGroupId = obj.BloodGroupId,
                        //BloodGroupName = obj.BloodGroupName
                    };
                    lstUsers.Add(user);
                }
                return lstUsers;
            }
            catch (Exception ex) { return new List<UserDto>(); }
        }

        public UserDto Inserted(UserDto user)
        {
            try
            {
                User obj = new User()
                {
                    Id = user.Id,
                    Username = user.Username,
                    Password = user.Password,
                    ConfimPassword = user.ConfirmPassword,
                    Email = user.Email,
                    FullName = user.FullName,
                    Address = user.Address,
                    City = user.City,
                    State = user.State,
                    Gender = (Boolean)user.Gender,
                    Phone = user.Phone,
                    Birthday = (DateTime)user.Birthday,
                    IdBloodGroup = user.BloodGroupId
                };
                contextDB.Users.Add(obj);
                contextDB.SaveChanges();
                return user;
            }
            catch (Exception ex) { return new UserDto(); }
        }

        public void Updated(UserDto user)
        {
            try
            {
                User objUser = contextDB.Users.First(attr => attr.Id == user.Id);
                objUser.Id = user.Id;
                objUser.Username = user.Username;
                objUser.Password = user.Password;
                objUser.ConfimPassword = user.ConfirmPassword;
                objUser.Email = user.Email;
                objUser.FullName = user.FullName;
                objUser.Address = user.Address;
                objUser.City = user.City;
                objUser.State = user.State;
                objUser.Gender = (Boolean)user.Gender;
                objUser.IdBloodGroup = user.BloodGroupId;
                objUser.Phone = user.Phone;
                objUser.Birthday = (DateTime)user.Birthday;
                contextDB.SaveChanges();
            }
            catch (Exception ex) { }
        }

        public void Deleted(UserDto user)
        {
            try
            {
                User objUser = contextDB.Users.First(i => i.Id == user.Id);
                contextDB.Users.Remove(objUser);
                contextDB.SaveChanges();
            }
            catch (Exception ex) { }
        }

    }
}
